package com.morn.fanfou.widget;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.morn.fanfou.ConfigActivity;
import com.morn.fanfou.FanfouApplication;
import com.morn.fanfou.R;
import com.morn.fanfou.api.Fanfou;
import com.morn.fanfou.http.ImageTool;
import com.morn.fanfou.model.Status;
import com.morn.fanfou.model.User;

public class UserListAdapter extends BaseAdapter {
	private List<User> userList;
	private LayoutInflater inflater;
	private int resource = R.layout.userlist_item;
	private Context context;
	private ImageTool asyncImageDownloader;
	private boolean isShowUserPhoto;

	private Fanfou fanfou;

	public UserListAdapter(Context context, List<User> userList) {
		this.context = context;
		this.userList = userList;
		inflater = LayoutInflater.from(context);
		Activity d = (Activity) context;
		fanfou = (((FanfouApplication) d.getApplication()).getFanfou());
		// asyncImageDownloader = new ImageTool(fanfou.getHttpClient(),
		// context);
		isShowUserPhoto = ConfigActivity.isShowUserPhoto(context);
	}

	@Override
	public void notifyDataSetChanged() {
		isShowUserPhoto = ConfigActivity.isShowUserPhoto(context);
		super.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return userList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		User user = userList.get(position);
		ViewHolder holder;
		View vi = convertView;
		if (vi == null) {
			vi = inflater.inflate(resource, null);
			holder = new ViewHolder();
			holder.photo = (ImageView) vi.findViewById(R.id.userListItemImage);
			holder.name = (TextView) vi.findViewById(R.id.userListItemNameText);
			holder.text = (TextView) vi.findViewById(R.id.userListItemText);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}
		holder.name.setText(user.getName());

		Status status = userList.get(position).getStatus();
		if (status != null) {
			holder.text.setText(status.getText());
		} else {
			holder.text.setText("");
		}
		if (isShowUserPhoto) {
			holder.photo.setVisibility(View.VISIBLE);
			asyncImageDownloader.download(user.getProfile_image_url(), holder.photo);
		} else {

			holder.photo.setVisibility(View.GONE);
		}
		return vi;
	}

	class MyButtonListener implements OnClickListener {
		private int position;

		MyButtonListener(int pos) {
			position = pos;
		}

		@Override
		public void onClick(View v) {
			Status status = userList.get(position).getStatus();
			if (status != null) {
				Toast.makeText(context, status.getText(), Toast.LENGTH_SHORT).show();
			}
		}
	}

	public static class ViewHolder {
		public ImageView photo;
		public TextView name;
		public TextView text;
	}

}
